/*
 * @Author: qianmingtian
 * @Date: 2020-07-17 11:18:43
 * @LastEditors: qianmingtian
 * @LastEditTime: 2020-07-17 11:59:40
 * @FilePath: \qianmingtian.github.io\code\java\MultiThread\exam\exam04\ExamThreadSynchronized04.java
 */
package MultiThread.exam.exam04;

/**
 * 题目：doother方法需要等待dosome方法结束吗？
 * 
 *  需要  类锁  前面几个是对象锁
 * 
 * 锁的是对象的类 ，
 *  
 */
public class ExamThreadSynchronized04 {

    public static void main(String[] args) {
        MyClass myClass1 = new MyClass();
        MyClass myClass2 = new MyClass();

        Thread t1 = new MyThread(myClass1);

        Thread t2 = new MyThread(myClass2);

        t1.setName("t1");
        t2.setName("t2");

        t1.start();


        t2.start();
    }

}

class MyClass {
    
// synchronized 修饰静态方法，作为类锁
    public synchronized static  void doSome() {
        System.out.println("doSome begin");

  

        System.out.println("doSome end");

    }

    public synchronized static void doOther() {
        System.out.println("do Other begin");

        System.out.println("do other end");
    }
}

class MyThread extends Thread {

    private MyClass myClass;

    MyThread(MyClass myClass) {
        this.myClass = myClass;
    }

    @Override
    public void run() {
        if (Thread.currentThread().getName().equals("t1")) {
            myClass.doSome();
        }
        if (Thread.currentThread().getName().equals("t2")) {
            myClass.doOther();
        }

    }

}
